<?php
/**
 * Created by PhpStorm.
 * User: kai.yang
 * Date: 2019-01-01
 * Time: 15:26
 */

namespace Home\Controller\Login;

use Model\LoginModel;
use Think\Controller;
use Utils\LoginUtils;

class AdminController extends Controller
{

    public function login(){
        $this->display('login:login');
    }


    public function doLogin(){
        //如果已经登录，直接进入首页
        $loginVo=session('loginVo');
        if(!empty($loginVo)){
            $this->redirect("/User/Admin/query");
        }

        $loginName=I("post.loginName");
        $password=I("post.password");
        if(empty($loginName)){
            $checkFailMsg="用户名不能为空!";
        }

        if(empty($password)){
            $checkFailMsg="密码不能为空!";

        }


        if(empty($checkFailMsg)){
            $checkLogin=new LoginUtils();

            $result= LoginUtils::checkLogin($loginName,$password);
            if(!$result instanceof LoginModel){
                $checkFailMsg=$result->getMsg();
            }

        }

        if(empty($checkFailMsg)){
            session('loginVo',$result);
            $this->redirect("/User/Admin/query");

        }else{
            $this->assign("checkFailMsg",$checkFailMsg);
            $this->display('login:login');
        }

    }

    public function loginOut(){

        $loginVo=session('loginVo');
        if(!empty($loginVo)){
            session('loginVo',null);
        }
        $this->display('login:login');


    }

}